package com.zx.map.service.impl;

import com.zx.map.dao.BillCheckLogDao;
import com.zx.map.dao.SummaryInfoDao;
import com.zx.map.dao.model.BillCheckLog;
import com.zx.map.dao.model.BillCheckLogExample;
import com.zx.map.dao.model.SummaryInfo;
import com.zx.map.service.BillCheckLogService;
import com.zx.map.service.domain.BillCheckLogDomain;
import com.zx.map.service.model.BillCheckLogModel;
import com.zx.platform.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 14-3-1 Time: 上午11:07 To
 * change this template use File | Settings | File Templates.
 */
@Component("billCheckLogService")
public class BillCheckLogServiceImpl implements BillCheckLogService {

    @Autowired
    private BillCheckLogDao dao;

    @Autowired
    private SummaryInfoDao summaryInfoDao;


    /**
     * 得到所有数据列表
     */
    public List getAllList() throws DataAccessException {

        List<BillCheckLog> list = dao.getListByExample(null);
        List<BillCheckLogModel> modelList = new ArrayList<BillCheckLogModel>();
        for (BillCheckLog o : list) {
            BillCheckLogModel temp = new BillCheckLogModel();
            BeanUtils.copyProperties(o, temp);
            modelList.add(temp);
        }
        return modelList;

    }

    /**
     * 根据单Key查找Model
     */
    public BillCheckLogModel getModelByKey(Integer key) throws DataAccessException {
        BillCheckLog o = dao.selectByPrimaryKey(key);
        BillCheckLogModel model = new BillCheckLogModel();
        BeanUtils.copyProperties(o, model);
        return model;
    }

    /**
     * 根据Model查找列表
     */
    public List getListByModel(BillCheckLogModel model) throws DataAccessException {
        BillCheckLogExample example = new BillCheckLogExample();
        com.zx.map.dao.model.BillCheckLogExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(model.getBillType())) {
            criteria.andBillTypeEqualTo(model.getBillType());
        }
        List<BillCheckLog> list = dao.getListByExample(example);
        List<BillCheckLogModel> modelList = new ArrayList<BillCheckLogModel>();
        for (BillCheckLog o : list) {
            BillCheckLogModel temp = new BillCheckLogModel();
            BeanUtils.copyProperties(o, temp);
            modelList.add(temp);
        }
        return modelList;
    }

    /**
     * 新增
     */
    public Integer add(BillCheckLogModel model) throws DataAccessException {
        BillCheckLog temp = new BillCheckLog();
        BeanUtils.copyProperties(model, temp);
        Integer key = dao.add(temp);
        return key;
    }

    /**
     * 修改
     */
    public Integer update(BillCheckLogModel model) throws DataAccessException {
        BillCheckLog temp = new BillCheckLog();
        BeanUtils.copyProperties(model, temp);
        dao.update(temp);
        return model.getBillId();
    }

    /**
     * 删除
     */
    public void delete(BillCheckLogModel model) throws DataAccessException {
        BillCheckLog temp = new BillCheckLog();
        BeanUtils.copyProperties(model, temp);
        dao.delete(temp);
    }

    /**
     * 分页查询
     *
     * @param pageSize
     * @param pageNum
     * @param domain
     * @return
     * @throws DataAccessException
     */
    public Map getPageByDomain(int pageSize, int pageNum, BillCheckLogDomain domain)
            throws DataAccessException {

        Map<String, Object> map = new HashMap<String, Object>();
        BillCheckLogExample example = new BillCheckLogExample();
        com.zx.map.dao.model.BillCheckLogExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(domain.getBillType())) {
            criteria.andBillTypeEqualTo(domain.getBillType());
        }
        if (domain.getBegindate() != null) {
            String begindateStr = DateUtils.dateToString(domain.getBegindate(), "yyyyMMdd");
            criteria.andBillDateGreaterThanOrEqualTo(begindateStr);
        }
        if (domain.getEnddate() != null) {
            String enddateStr = DateUtils.dateToString(domain.getEnddate(), "yyyyMMdd");
            criteria.andBillDateLessThanOrEqualTo(enddateStr);
        }
        
        if (null != domain.getBillDate()) {
            criteria.andBillDateEqualTo(domain.getBillDate());
        }
        //排序add by wenjw 2015.6.2
        example.setOrderByClause("create_Date desc");
        int pageBegin = (pageNum - 1) * pageSize;
        List<BillCheckLog> list = dao.getPageByExample(pageSize, pageBegin, example);
        List<BillCheckLogModel> menuModelList = new ArrayList<BillCheckLogModel>();
        for (BillCheckLog o : list) {
            BillCheckLogModel temp = new BillCheckLogModel();
            BeanUtils.copyProperties(o, temp);
            menuModelList.add(temp);
        }
        int count = dao.countByExample(example);
        map.put("count", count);
        map.put("list", menuModelList);
        return map;
    }

    /**
     * 入款对账
     * @param currentDate
     */
    public void incomeBillCheck(String currentDate) throws DataAccessException{
        //1、获取我方系统 总入款金额 笔数
        SummaryInfo summaryInfo = summaryInfoDao.getOrdlogSummary(currentDate);
        //2、获取对方系统 总笔数金额
        SummaryInfo oppSummaryInfo = summaryInfoDao.getPaymentSummary(currentDate);
        //3、保存数据  先删除 再新增
        BillCheckLog billCheckLog = new BillCheckLog();
        billCheckLog.setBillDate(currentDate);
        billCheckLog.setBillType("1");//入款对账
        billCheckLog.setCreateDate(new Date());
        billCheckLog.setTradeCount(summaryInfo.getSumCount());
        billCheckLog.setTradeSum(summaryInfo.getSumAmt());
        billCheckLog.setOppTradeCount(oppSummaryInfo.getSumCount());
        billCheckLog.setOppTradeSum(oppSummaryInfo.getSumAmt());
        billCheckLog.setDiffCount(new BigDecimal(oppSummaryInfo.getSumCount()).subtract(new BigDecimal(summaryInfo.getSumCount())).intValue());
        billCheckLog.setDiffSum(new BigDecimal(oppSummaryInfo.getSumAmt()).subtract(new BigDecimal(summaryInfo.getSumAmt())).doubleValue());
        BillCheckLogExample example = new BillCheckLogExample();
        BillCheckLogExample.Criteria criteria = example.createCriteria();
        criteria.andBillDateEqualTo(currentDate);
        criteria.andBillTypeEqualTo("1");
        List<BillCheckLog> billCheckLogList = dao.getListByExample(example);
        if (null != billCheckLogList && billCheckLogList.size() == 1) {
            billCheckLog.setBillId(billCheckLogList.get(0).getBillId());
            billCheckLog.setUpdateDate(new Date());
            dao.update(billCheckLog);
        } else {
            dao.add(billCheckLog);
        }
    }
    
    /**
     * 出款对账
     * @param currentDate
     */
    public void outcomeBillCheck(String currentDate) throws DataAccessException{
        //1、获取我方系统 总结算金额 笔数
        SummaryInfo summaryInfo = summaryInfoDao.getOrdlogSettleSummary(currentDate);
        //2、获取对方系统 总笔数金额
        SummaryInfo oppSummaryInfo = summaryInfoDao.getSettleSummary(currentDate);
      //3、获取对方系统 总笔数金额
        SummaryInfo adjustSummaryInfo = summaryInfoDao.getAdjustSummary(currentDate);
      //4、获取对方系统 总笔数金额
        SummaryInfo manualSummaryInfo = summaryInfoDao.getManualSummary(currentDate);
        //5、保存数据  先删除 再新增
        BillCheckLog billCheckLog = new BillCheckLog();
        billCheckLog.setBillDate(currentDate);
        billCheckLog.setBillType("2");//出款对账
        billCheckLog.setCreateDate(new Date());
        BigDecimal tradeCount = new BigDecimal(summaryInfo.getSumCount()).add(new BigDecimal(adjustSummaryInfo.getSumCount()))
        .add(new BigDecimal(manualSummaryInfo.getSumCount()));
        BigDecimal tradeAmt = new BigDecimal(summaryInfo.getSumAmt()).add(new BigDecimal(adjustSummaryInfo.getSumAmt()))
        .add(new BigDecimal(manualSummaryInfo.getSumAmt()));
        billCheckLog.setTradeCount(tradeCount.intValue());
        billCheckLog.setTradeSum(tradeAmt.doubleValue());
        billCheckLog.setOppTradeCount(oppSummaryInfo.getSumCount());
        billCheckLog.setOppTradeSum(oppSummaryInfo.getSumAmt());
        billCheckLog.setDiffCount(new BigDecimal(oppSummaryInfo.getSumCount()).subtract(tradeCount).intValue());
        billCheckLog.setDiffSum(new BigDecimal(oppSummaryInfo.getSumAmt()).subtract(tradeAmt).doubleValue());
        BillCheckLogExample example = new BillCheckLogExample();
        BillCheckLogExample.Criteria criteria = example.createCriteria();
        criteria.andBillDateEqualTo(currentDate);
        criteria.andBillTypeEqualTo("2");
        List<BillCheckLog> billCheckLogList = dao.getListByExample(example);
        if (null != billCheckLogList && billCheckLogList.size() == 1) {
            billCheckLog.setBillId(billCheckLogList.get(0).getBillId());
            billCheckLog.setUpdateDate(new Date());
            dao.update(billCheckLog);
        } else {
            dao.add(billCheckLog);
        }
    }
}
